//index.js
//获取应用实例
const app = getApp()
const activityService = require('../../service/activity.js');

Page({
  data: {
    title:"活动详情",
    id: "",
    detailInfo: {},
    dialogVisible: false,
    loading:false,
    form_info:""
  },
  onLoad: function (options) {
    let id = options.id;
    this.setData({
      id: id
    })
    let d = {
      activityId: id
    }
    this.getContentList(d);
  },
  getContentList(d) {
    activityService.getActivityDetail(d).then(res => {
      if (res.resCode == 0) {
        this.setData({
          detailInfo: res.object
        });
      }
    })
  },
  formSubmit(e){
    console.log(e.detail.value);
    let obj = e.detail.value;
    for(let k in obj){
      if(String(obj[k]).trim() == ""){
        wx.showToast({
          title: '请填写全信息',
          icon:"none"
        })
        return false;
      }
    }
    this.setData({
      loading:true
    });
    let d = {
      objectId: this.data.detailInfo.activityId,
      ...obj
    }
    activityService.getActivityEnroll(d).then(res => {
      if(res.resCode == 0){
        wx.showToast({
          title: '报名成功',
          icon:"none"
        })
        this.toggleApply();
        this.resetContent();
      }
    })
  },
  toggleApply(){
    if(app.checkLogin()){
      this.setData({
        dialogVisible: !this.data.dialogVisible,
        loading:false
      })
    }
  },
  resetContent(){
    this.setData({
      form_info:""
    })
  },
  getShareCover(str = ""){
    let strArr = str.split(",");
    return `${app.globalData.$config.IMAGE_BASE_URL}${strArr[0]}.jpg`
  },
  getShareTitle(str = "",len = 50){
    return str.slice(0,len);
  },
  onShareAppMessage(options){
    return {
      imageUrl: this.getShareCover(this.data.detailInfo.activityImages),
      title: this.getShareTitle(this.data.detailInfo.activityName)
    }
  },
})
